package com.itxx.thread4;
    //同步代码块

public class TicketDemo implements Runnable {
    private static int sticket = 100;
    private static Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (sticket == 0) {
                    System.out.println(Thread.currentThread().getName() + ":票卖完了");
                    break;
                } else if (sticket > 0) {
                    try {
                        Thread.sleep(100);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }System.out.println(Thread.currentThread().getName() + "正在卖" + sticket + "张票");
                    sticket--;
                }
            }
        }
    }
}

class Demo {
    public static void main(String[] args) {
        TicketDemo tt = new TicketDemo();
        Thread t1 = new Thread(tt, "窗口1");
        Thread t2 = new Thread(tt, "窗口2");
        Thread t3 = new Thread(tt, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

